package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.accumulated;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.QuotaConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.StrategyConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.AcceptRequestDataModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 累计风险保额校验 HCT018
 *
 * @author :  zlc
 * @date : 2019/9/24 10:54
 **/
public class Hct017AccumulatedStrategy implements AccumulatedStrategy {

    @Override
    public InsuredModel checkAccumulatedRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository,
                                                        ProductModel productModel, RedisService redisService, ProductConfig productConfig) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        Date valiDate = requestModel.getPolicyInfo().getCvalidate();
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        int prodInsSeq = productModel.getProdInsSeq();
        for (InsurantModel insurantModel : insurantModelList) {
            // 被保人序号和产品序号是否一致
            if (insurantModel.getInsSeq() == prodInsSeq) {
                //校验被保人累计重疾险保额是否超标
                //公式 = 历史单+当前险种的风险保额
                BigDecimal nowTotal = BigDecimal.valueOf(0);
                StringBuilder sb = new StringBuilder(16);
                List<ProductModel> productModelList = requestModel.getProductInfo().getProducts().getProduct();
                for (ProductModel product : productModelList) {
                    // 是否为同一人的产品
                    if (insurantModel.getInsSeq() != product.getProdInsSeq()) {
                        continue;
                    }
                    sb.setLength(0);
                    sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(product.getProdCode());
                    Object obj = redisService.get(sb.toString());
                    if (obj != null) {
                        String[] a = String.valueOf(obj).split("\\|");
                        //  当前保单1或2标识为豁免险，不进行非豁免险累计
                        if (!(StringUtils.equals(a[4], "1") || StringUtils.equals(a[4], "2"))) {
                            // 当前风险保额
                            //if(productConfig.getAccumulatedRiskList().contains(product.getProdCode())){
                            if (AcceptUtil.isStrategy(StrategyConstant.CRITICAL_ILLNESS_INSURANCE,
                                    product.getProdCode(), productConfig.getAccumulatedRiskList())) {
                                nowTotal = nowTotal.add(AcceptUtil.sumInsured(product.getProdCode(), product.getAmnt()));
                            }
                        }
                    }
                }
                // 获取历史单保费
                BigDecimal cacheTotal = redisService.getCacheQuota(QuotaConstant.SORT_A, insurantModel.getIdNo(), nowTotal, requestModel.getPolicyInfo().getContNo());
                double historyTotal = insuredCoreRepository.getSeriousDiseaseInsuranceCount(insurantModel.getInsuredNo(), insurantModel.getInsuredNo());

                Date birthday = insurantModel.getBirthday();
                //当前险种的风险保额
                BigDecimal total = nowTotal.add(BigDecimal.valueOf(historyTotal)).add(cacheTotal);
                //获取日期相差的天数
                int years = DateUtil.getYears(birthday, valiDate);
                int days = DateUtil.getDays(birthday, valiDate);
                //
                if (days >= UnderWriteConstant.AGE_THIRTY_DAY && years <= UnderWriteConstant.AGE_FORTY_YEAR && AcceptUtil.compareTo(total, BigDecimal.valueOf(500000d)) == 1) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0012_MESSAGE, insurantModel.getName()));
                } else if (years >= UnderWriteConstant.AGE_FORTY_SIX_YEAR && years <= UnderWriteConstant.AGE_FIFTY_YEAR && AcceptUtil.compareTo(total, BigDecimal.valueOf(200000d)) == 1) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0009_MESSAGE, insurantModel.getName()));
                } else if (years >= UnderWriteConstant.AGE_FORTY_ONE_YEAR && years <= UnderWriteConstant.AGE_FORTY_FIVE_YEAR && AcceptUtil.compareTo(total, BigDecimal.valueOf(300000d)) == 1) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0018_MESSAGE, insurantModel.getName()));
                }
            }
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
}
